package com.airnewslinkosky.ticket.ui.client;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.PassengerInfoResult;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.Constants;
import com.mobclick.android.MobclickAgent;
/**
 * 大客户下的常旅客查询页
 * @author toshiba
 *
 */
public class ClientContactsSearch  extends BaseActivity implements OnClickListener{
	public final int NORESULT = 3;
	public final int NOORDER = 4;
	public final int NOORDERSTAT = 5;
	public final int NET_ERROR=6;
	public final int SEARCH_ERROR=7;
	private ProgressDialog progressDialog = null;
	public Handler myHandler;
	private Toast toast;
	private SharedPreferences sp;
	
	public  final int DIALOG_IDTYPE_LIST = 10;
	
	//证件类型,生日
	private TextView cardType;
	private LinearLayout cardTypeLayout;
	
	private Button addBut,searchBut,clearAll;
	private EditText userName,mobile;
	
	public static String customerNo = "";
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.client_contactor_query);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
        
		activityList.add(this);
		
		userName = (EditText) findViewById(R.id.userName);
		mobile = (EditText) findViewById(R.id.mobile);
		
		cardTypeLayout = (LinearLayout)findViewById(R.id.cardTypeLayout);
		cardType =  (TextView)findViewById(R.id.cardType);
		cardTypeLayout.setOnClickListener(this);
		
		addBut = (Button) findViewById(R.id.addBut);
		addBut.setOnClickListener(this);
		
		searchBut = (Button) findViewById(R.id.searchBut);
		searchBut.setOnClickListener(this);
		
		clearAll = (Button) findViewById(R.id.clearAll);
		clearAll.setOnClickListener(this);
		
		findViewById(R.id.backBut).setOnClickListener(this);
		
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case NORESULT:
					toast = Toast.makeText(ClientContactsSearch.this, "未找到符合条件的常旅客", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case NOORDER:
					toast = Toast.makeText(ClientContactsSearch.this, "无常旅客信息", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case NET_ERROR:
					toast = Toast.makeText(ClientContactsSearch.this, "网络超时，连接失败", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case SEARCH_ERROR:
					toast = Toast.makeText(ClientContactsSearch.this,  (String)msg.obj, Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}
				super.handleMessage(msg);
			}
		};
		
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.addBut:
				Intent intent = new Intent();
				intent.setClass(ClientContactsSearch.this, ClientContactsAdd.class);
				startActivity(intent);
				break;
			case R.id.searchBut:
				search();
				break;
			case R.id.clearAll:
				break;
			case R.id.backBut: 
				Intent intentR = new Intent();
				intentR.setClass(ClientContactsSearch.this, ClientList.class);
				startActivity(intentR);
				break;
			case R.id.cardTypeLayout:
				showDialog(DIALOG_IDTYPE_LIST);
				break;
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		    case DIALOG_IDTYPE_LIST:
	            return new AlertDialog.Builder(ClientContactsSearch.this)
	                .setIcon(R.drawable.ic_menu_more)
	                .setTitle("请选择")
	                .setSingleChoiceItems(R.array.paperTypes, 0, new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    	String[] arrs = getResources().getStringArray(R.array.paperTypes);
	                    	cardType.setText(arrs[whichButton]);
	                    	dialog.dismiss();
	                        /* User clicked on a radio button do some stuff */
	                    }
	                })
	                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    
	                    }
	                })
	                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    }
	                })
	               .create();
		    default:
				return null;
		}
	}
	
	
	private void search() {
		
		progressDialog = ProgressDialog.show(this, "请稍等...", "查询常旅客中...",true);
		final Thread thread1 = new Thread() {
			public void run() {
//				sp = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//		        String userIdLogin = sp.getString("userIdB2B", null);
//		        String userPwd =  sp.getString("userPwdB2B", null);
//		        String asToken =   sp.getString("ASTokenB2B", null);
				String userIdLogin = Constants.USERID;
		        String userPwd =  Constants.USERPWD;
		        String asToken =  Constants.ASToken;
		        
		        PassengerInfoResult passengerInfoResult = WebAccessor.searchPassengerInfoManage(ClientContactsSearch.this,userIdLogin,userPwd,asToken,"",String.valueOf(userName.getText()),"",String.valueOf(mobile.getText()),"");
				
				if (progressDialog.isShowing()) {
					if (passengerInfoResult== null) {
						Message msg = new Message();
						msg.what = NORESULT;
						myHandler.sendMessage(msg);
					}else if(!"0".equals(passengerInfoResult.Result)){
						Message msg = new Message();
						msg.what = SEARCH_ERROR;
						msg.obj = passengerInfoResult.ErrorDescription;
						myHandler.sendMessage(msg);
					}else{
						Intent intentS = new Intent();
						intentS.setClass(ClientContactsSearch.this, ClientContactsList.class);
						ClientContactsList.passengerInfos = passengerInfoResult.passengerInfoList;
						ClientContactsList.isSearch = false;
						startActivity(intentS);
					}
					progressDialog.dismiss();
				}
			}
		};
		progressDialog.setCancelable(true);
		thread1.start();
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
